<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

class SellerBill extends DbEloquent{

    protected $table    = 'ygt_seller_bill';

    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'厂id','value'=>0,'is_must'=>1],
        ['name'=>'seller_company_id','title'=>'厂的供应商id','value'=>0,'is_must'=>1],
        ['name'=>'sn','title'=>'编号','value'=>'','is_must'=>1],
        ['name'=>'total_money','title'=>'金额','value'=>0,'is_must'=>1],
        ['name'=>'now_money','title'=>'金额','value'=>0,'is_must'=>1],
        ['name'=>'pay_type','title'=>'付款方式','value'=>'','is_must'=>1],
        ['name'=>'img_id','title'=>'附加图片','value'=>0,'is_must'=>0],
        ['name'=>'add_user_id','title'=>'登录出错','value'=>0,'is_must'=>1],
    ];

    public static function getJoinListByType($where, $type=1, $limit='', $offset='', $orderby=['ygt_seller_bill.id','desc'])
    {
        switch ($type)
        {
            case 1:
                //财务模块->应付款->供应商的账单(付款单)
                //供应商模块->账单列表(付款单和应付单)
                //where条件是不一样的
                $fileds             = 'ygt_seller_bill.id,ygt_seller_bill.sn,ygt_seller_bill.total_money';
                $fileds             .= ',ygt_seller_bill.now_money,ygt_seller_bill.pay_type';
                $fileds             .= ',ygt_seller_bill.img_id,ygt_seller_bill.status';
                $fileds             .= ',ygt_seller_bill.created_at';
                $fileds             .= ',ygt_seller_bill.type';
                $fileds             .= ',ygt_seller_company.title as seller_company_name';
                $join               = [
                    ['table'=>'ygt_seller_company', 'field_l'=>'ygt_seller_company.id', 'field_c'=>'=', 'field_r'=>'ygt_seller_bill.seller_company_id']
                ];
                break;
            default:
                $fileds             = '';
                $join               = '';
        }
        $groupby            = '';
        $result             = self::getList($where, $fileds, $limit, $offset, $orderby, $groupby, $join);
        return $result;
    }
    //默认插入一个应付单
    public static function insertOneDataByType($data,$pk='',$type=2)
    {
        switch ($type)
        {
            case 1:
                $result             = false;
                break;
            case 2:
                //插入一个应付单
                $addUserId          = $data['add_user_id'];
                $sn                 = 'YFD'.$_SERVER['REQUEST_TIME'].$addUserId.mt_rand(1000,9999);
                $insertData         = [
                    'type'=>2,
                    'sn'=>$sn,
                    'img_id'=>0,
                    'company_id'=>$data['company_id'],
                    'seller_company_id'=>$data['seller_company_id'],
                    'total_money'=>$data['money'],
                    'now_money'=>$data['money'],
                    'pay_type'=>$data['pay_type'],
                    'add_user_id'=>$addUserId,
                ];
                $result             = self::insertOneData($insertData,$pk);
                break;
            case 3:
                //插入一个采购的退款单
                $addUserId          = $data['add_user_id'];
                $sn                 = 'TKD'.$_SERVER['REQUEST_TIME'].$addUserId.mt_rand(1000,9999);
                $insertData         = [
                    'type'=>3,
                    'sn'=>$sn,
                    'img_id'=>0,
                    'company_id'=>$data['company_id'],
                    'seller_company_id'=>$data['seller_company_id'],
                    'total_money'=>$data['money'],
                    'now_money'=>$data['money'],
                    'pay_type'=>$data['pay_type'],
                    'add_user_id'=>$addUserId,
                ];
                $result             = self::insertOneData($insertData,$pk);
                break;
            default:
                $result             = false;
        }
        return $result;
    }

}
